package com.luxsoft.em2.replication.central;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import com.luxsoft.siipap.util.DBUtils;
import com.luxsoft.utils.LoggerHelper;

public class ReplicadorCentralManager {
	
	public static  ReplicadorCentralManager INSTANCE;
	
	Logger logger=LoggerHelper.getLogger();
	
	ApplicationContext context;
	
	private ReplicadorCentralManager(){
		initContext();
	}
	
	private void initContext(){
		DBUtils.whereWeAre();
		logger.info("Inicianlizando spring context para replicacion central JMS...");
		context=new ClassPathXmlApplicationContext(
				new String[]{
						"spring/jms-replica-centralManager-context.xml",
						 "spring/jms-replica-import-queues-context.xml"
						//,"spring/jms-replica-import-queues-context.xml"
						
						}
				);
		System.out.println("Conectado a: ");
		
		
	}
	
	public List<String[]> getBrokers(){
		List<String[]> brokers=new ArrayList<String[]>();
		Map<String,Object> beansMap=context.getBeansOfType(DefaultMessageListenerContainer.class);
		Set<Map.Entry<String,Object>> set=beansMap.entrySet();
		for(Map.Entry<String, Object> entry:set){
			DefaultMessageListenerContainer c=(DefaultMessageListenerContainer)entry.getValue();
			ActiveMQConnectionFactory fac=(ActiveMQConnectionFactory)c.getConnectionFactory();
			brokers.add(new String[]{entry.getKey(),fac.getBrokerURL()});
		}
		return brokers;
	}
	
	public static ReplicadorCentralManager getInstance(){
		if(INSTANCE==null){
			INSTANCE=new ReplicadorCentralManager();
		}
		return INSTANCE;
	}
	
	public static void main(String[] args) throws Exception{
		DBUtils.whereWeAre();
		ReplicadorCentralManager manager=getInstance();
		manager.getBrokers();
	}

}
